

#import "StartFigureView.h"

@implementation StartFigureView

// 广告显示的时间
static int const showtime = 5;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
//        int WID = kScreenWidth;
//        int HEI = kScreenHeight;
        
        UIView *VIEW = [[UIView alloc]initWithFrame:self.bounds];
        VIEW.backgroundColor = KRedColor;
        [self addSubview:VIEW];
      
        UIImage *LaunchImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:LaunchImageUrl]]];
//        UIImage *LaunchImage = [UIImage imageNamed:@"LaunchImageUrl.jpeg"];

        _lauImageVW = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _lauImageVW.backgroundColor = KRedColor;
        _lauImageVW.userInteractionEnabled = YES;
        _lauImageVW.image = LaunchImage;
        [self addSubview:_lauImageVW];

        _advBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-120,44,90,30)];
        [_advBtn setHidden:NO];
//        [_advBtn setTitle:[NSString stringWithFormat:@"跳过%d", showtime] forState:UIControlStateNormal];
        //特殊处理
        [_advBtn setTitle:[NSString stringWithFormat:@"跳过"] forState:UIControlStateNormal];
        _advBtn.titleLabel.font = CUSTOMFONT(@"MarkerFelt-Thin", 15);
        _advBtn.layer.masksToBounds = YES;
        _advBtn.layer.cornerRadius = _advBtn.frame.size.height/2;
        _advBtn.layer.borderWidth = 1;
        _advBtn.layer.borderColor = KWhiteColor.CGColor;
        [_advBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchDown];
        [_lauImageVW addSubview:_advBtn];

        _count = showtime;
        _countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
        /*
        [NSThread sleepForTimeInterval:2.0];//休眠2秒
        */
        //5秒后自动关闭

//        self.progressview=[[UIProgressView alloc]initWithFrame:CGRectMake((kScreenWidth-250)/2, kScreenHeight-70, 250, 5)];
//        self.progressview.backgroundColor=[UIColor clearColor];
//        [self.progressview setProgressImage:[UIImage imageNamed:@"progress.png"]];
//        [self addSubview:self.progressview];


//
//        self.timer=[NSTimer scheduledTimerWithTimeInterval:0.0008 target:self selector:@selector(loading:) userInfo:nil repeats:YES];
//        [self.timer fire];//开启定时器

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(showtime*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismiss];
        });
    }
    return self;
}

//-(void)loading:(NSTimer*)timer
//{
//    if(self.progressview.progress!=1)
//    {
//        self.progressview.progress+=0.0008f;
//    }
//    else
//    {
//        [self dismiss];
//    }
//}

- (void)countDown
{
    _count --;
//    [_advBtn setTitle:[NSString stringWithFormat:@"跳过%d",_count] forState:UIControlStateNormal];
    //特殊处理
    [_advBtn setTitle:[NSString stringWithFormat:@"跳过"] forState:UIControlStateNormal];
    if (_count <= 0) {

        [self dismiss];

    }
}

// 移除广告页面
- (void)dismiss
{
    [_countTimer invalidate];
    _countTimer = nil;
    [UIView animateWithDuration:0.3f animations:^{

        self.alpha = 0.f;

    } completion:^(BOOL finished) {

        [self removeFromSuperview];

    }];
}

@end
